/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Menu.java
 *
 * Created on 16/05/2012, 05:46:11 PM
 */
package Serializacion_Ejercicios2_3;
import java.io.*;
import javax.swing.*; 

/**
 *
 * @author Miguel
 */
public class Menu extends javax.swing.JFrame {

    /** Creates new form Menu */
    public Menu() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        TxtNombre = new javax.swing.JTextField();
        TxtApellidoP = new javax.swing.JTextField();
        TxtApellidoM = new javax.swing.JTextField();
        TxtDireccion = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        BMostrar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TADatos = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("FormPrincipal"); // NOI18N

        jTextField1.setText("jTextField1");
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        TxtNombre.setName("TxtNombre"); // NOI18N

        jLabel1.setText("Nombre");

        jLabel2.setText("Apellido Paterno");

        jLabel3.setText("Apellido Materno");

        jLabel4.setText("Direccion:");

        jButton1.setText("Agregar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        BMostrar.setText("Listar");
        BMostrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BMostrarActionPerformed(evt);
            }
        });

        TADatos.setColumns(20);
        TADatos.setRows(5);
        jScrollPane1.setViewportView(TADatos);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addGap(95, 95, 95)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(47, 47, 47)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 159, Short.MAX_VALUE)
                                        .addGap(89, 89, 89))
                                    .addComponent(TxtApellidoP, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(27, 27, 27)
                                .addComponent(jButton1)
                                .addGap(87, 87, 87)
                                .addComponent(BMostrar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(22, 22, 22))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TxtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(TxtApellidoM, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39))
            .addGroup(layout.createSequentialGroup()
                .addGap(100, 100, 100)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 493, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(87, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TxtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TxtApellidoM, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TxtApellidoP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(TxtDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BMostrar)
                    .addComponent(jButton1))
                .addGap(34, 34, 34)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(155, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    String Nombre = TxtNombre.getText( );
    String ApellidoP = TxtApellidoP.getText( );
    String ApellidoM = TxtApellidoM.getText( );
    String Direccion = TxtDireccion.getText( );
    
    Persona per = new Persona(Nombre,ApellidoP,ApellidoM,Direccion);
        
    try {
            //creamos el archivo
            FileOutputStream fs =
                    new FileOutputStream("ListaPersona.ser");
            //Esta clase tiene el metodo writeObject() que necesitamos
            ObjectOutputStream os =
                    new ObjectOutputStream(fs);
            os.writeObject(per);            
            //El metodo witeObject() serializa el objeto
            //y lo escribe en el archivo
            os.close();            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        JOptionPane.showMessageDialog (null, "Los objetos se han serializado correctamente");  
        //showMessage("Los objetos se han serializado correctamente");
        System.out.println("Los objetos se han serializado correctamente");
        
    //TxtSalida.setText(Nombre);
    //System.out.println(Nombre);

    //String nombre = FormPrincipal["TxtNombre"].value;
    //oTextarea = FormPrincipal.elements["TxtNombre"];
    /*valor_textarea = oTextarea.value;
    intereses = oForm.elements["intereses"].value;
    oHidden = oForm.elements["nombre_elemento_hidden"];
    valor_hidden = oHidden.value;
    oHidden = oForm.elements["id_seccion"].value;*/
    // TODO add your handling code here:
        TxtNombre.setText(null);
        TxtApellidoP.setText(null);
        TxtApellidoM.setText(null);
        TxtDireccion.setText(null);  
}//GEN-LAST:event_jButton1ActionPerformed

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jTextField1ActionPerformed



private String LeerArchivo(File f){
    StringBuffer texto=new StringBuffer();
    try {
            int c;
            FileReader entrada=new FileReader(f);
            while((c=entrada.read())!=-1){
            texto.append((char)c);
        }
    }catch (IOException ex) {}
        return texto.toString();
 }
 

private void BMostrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BMostrarActionPerformed
    // TODO add your handling code here:
    Persona Per = null;
        
    FileInputStream fis;
        try{
            // Recuperar un archivo de disco
            fis = new FileInputStream("ListaPersona.ser");
            // Stream para recuperar o reconstruir objetos
                        
            //byte[] c = new byte[ois.available()]; 
            boolean fin = false;
            //El metodo readObject() recupera el objeto
            while (fin == false)
            {
                ObjectInputStream ois = new ObjectInputStream(fis);
                Per = (Persona) ois.readObject();
                TADatos.append(Per.getNombre()+ "  " + Per.getP_Apellido()+ "  " + Per.getS_Apellido() + "  " +
                Per.getDireccion()+ "\n");
                if(ois.readObject() == null)
                {
                    fin = true; 
                    ois.close();                    
                }                  
            }
            
            
            /*String[] DatosPer = new String[c]; 
            DatosPer[c] = Per.getNombre()+ "  " + Per.getP_Apellido()+ "  " + Per.getS_Apellido() + "  " +
                    Per.getDireccion();
            TADatos.setText(DatosPer[1]);*/
                 
        
            /*InputStreamReader fis2 = new InputStreamReader(fis); 
            
        BufferedReader linea = new BufferedReader(fis2);
        while (linea.ready()) 
        {
            String linea_arch = linea.readLine();
            TADatos.setText(Per.getNombre()+ "  " + Per.getP_Apellido()+ "  " + Per.getS_Apellido() + "  " +
                    Per.getDireccion() + linea_arch + "\r\n");        
        }
        linea.close();  */  
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        
        //TADatos.setText(LeerArchivo("ListaPersona.ser"));        
        
        /*TADatos.append(Per.getNombre()+ "  " + Per.getP_Apellido()+ "  " + Per.getS_Apellido() + "  " +
        Per.getDireccion()+ "\n"); */
        
        TxtNombre.setText(null);
        TxtApellidoP.setText(null);
        TxtApellidoM.setText(null);
        TxtDireccion.setText(null);         
}//GEN-LAST:event_BMostrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Menu().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BMostrar;
    private javax.swing.JTextArea TADatos;
    private javax.swing.JTextField TxtApellidoM;
    private javax.swing.JTextField TxtApellidoP;
    private javax.swing.JTextField TxtDireccion;
    private javax.swing.JTextField TxtNombre;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
